<script setup>
import { onMounted, ref, watch } from "vue";
const $props = defineProps(["id"]);
import { queryComment } from "@/api/article.js";
import TextArea from "@/components/textArea.vue";
import Comment from "./Comment.vue";
import { addComment } from "@/api/article.js";
import { beforeTime } from "@/utils/timeInterval.js";
import {organizeComments2} from "@/utils/formatMessageList.js";

const replyList = ref([]);
onMounted(() => {
  console.log("重新mounted");
  getList();
});

watch(
  () => $props.id,
  () => {
    getList();
  }
);

const getList = async () => {
  console.log("id@@@@@@", $props.id);
  const res = await queryComment($props.id);
  // 找到到回复评论的那条信息
  let arr = organizeComments2(res.data);
  console.log(arr, "列表");
  replyList.value = arr;
};

const TextareaItemClose = (item) => {
  console.log(item, "2@@");
  item.show = false;
};

const handleSubmitReply = async (requestObj, item) => {
  await addComment(requestObj);
  getList();
  TextareaItemClose(item);
};

const handleChange = () => {
  getList();
};
</script>

<template>
  <Comment @change="handleChange" />
  <div class="reply-news" v-if="replyList.length > 0">
    <div
      class="news-item"
      v-for="item in replyList"
      :key="item.id"
      :class="{ replyClass: item.replyId ? true : false }"
      @mouseenter="() => (item.hover = true)"
      @mouseleave="() => (item.hover = false)"
    >
      <div class="news-user">
        <div class="left">
          <div class="pic">
            <img :src="item.authorInfo.avatar" alt="" />
          </div>
          <div class="info">
            <div class="name">{{ item.authorInfo.nickname }}</div>
            <div class="time">发布{{ beforeTime(item.createTime) }}</div>
          </div>
        </div>
        <div class="btn" :class="{ hover: item.hover ? true : false }">
          <el-button
            type="danger"
            size="small"
            @click="() => (item.show = true)"
            >回复</el-button
          >
        </div>
      </div>
      <div class="content" v-html="item.content"></div>
      <TextArea
        v-if="item.show"
        @close="TextareaItemClose(item)"
        :item="item"
        @submit="handleSubmitReply"
        type="2"
      />
    </div>
  </div>
  <div v-else>暂无评论</div>
</template>

<style scoped lang="scss">
.reply-news {
  margin-top: 10px;
  .news-item {
    margin-bottom: 10px;
    &.replyClass {
      padding-left: 20px;
    }
    .btn {
      opacity: 0;
      transition: all 0.2s ease;
      &.hover {
        opacity: 1;
        // display: block;
      }
    }
    .news-user {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;
      margin-bottom: 10px;
      .left {
        display: flex;
      }
    }
    .content {
      display: flex;
      align-items: center;
      background: #f5f5f5;
      padding: 10px;
    }
  }
}

.news-user {
  display: flex;
  align-items: center;

  .pic {
    margin-right: 10px;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .name {
    margin-right: 10px;
    color: $strong;
  }
  .time {
    font-size: 12px;
    color: #c5bdbd;
    margin-right: 10px;
  }
}
</style>
